<template>
  <view>
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-btn u-flex u-row-center" v-if="!address">
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          @click="handleChooseAddress"
          :custom-style="{ backgroundColor: '#fff!important' }"
          >获取收货地址</u-button
        >
      </view>

      <view v-else class="address-detail">
        <view class="address-detail">{{ addressDetail }}</view>
        <view class="address-user"
          >{{ address.userName }} {{ address.telNumber }}
        </view>
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 已选商品 -->
    <view class="cart">
      <view class="goods-title">已选商品</view>
      <!-- 1 列表 -->
      <view class="cart-list" v-for="item in goPayList" :key="item.goods_id">
        <view class="cart-item">
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">{{ item.nums }}</view>
              </view>
            </view>
          </view>
        </view>
      </view>
      <!-- 2 统计 -->
      <view class="statistics">
        <view class="all-price">
          合计: <text>￥{{ totalCount }}</text>
        </view>
        <view class="all-count">
          <u-button type="error" shape="circle" @click="cartGoPay"
            >去结算({{ totalNums }})</u-button
          >
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      address: null,
    };
  },
  computed: {
    ...mapState("user", ["token"]),
    ...mapGetters("cart", ["goPayList", "totalCount", "totalNums"]),
    // 详细地址
    addressDetail() {
      if (!this.address) return "";
      return (
        this.address.provinceName +
        this.address.cityName +
        this.address.countyName +
        this.address.detailInfo
      );
    },
  },
  methods: {
    ...mapActions("user", ["getToken"]),
    async handleChooseAddress() {
      const [err, res] = await uni.chooseAddress();
      if (!err) {
        this.address = res;
      }
    },
    async cartGoPay() {
      if (!this.address) {
        uni.showToast({
          title: "请选择地址",
          icon: "none",
        });
        return;
      }

      // 判断有没有token
      if (!this.token) {
        await this.getToken();
      }

      // 创建订单编号
      const orderParams = {
        order_price: this.totalCount,
        consignee_addr: `${this.addressDetail}|${this.address.userName}|${this.address.telNumber}`,
        goods: this.goPayList.map((item) => ({
          goods_id: item.goods_id,
          goods_number: item.nums,
          goods_price: item.goods_price,
        })),
      };
      // 获取订单编号
      const { order_number } = await this.$u.post(
        "/my/orders/create",
        orderParams
      );
      // console.log(res4);
      // 获取支付参数
      const { pay } = await this.$u.post("/my/orders/req_unifiedorder", {
        order_number,
      });
      //   console.log(res5);
      // 发起微信支付
      const res6 = await uni.requestPayment(pay);
      console.log(res6);
    },
  },
};
</script>

<style lang="scss">
.address {
  padding-bottom: 111rpx;
  .btn {
    display: flex;
    justify-content: center;
  }
}
.cart {
  padding-bottom: 111rpx;
  .goods-title {
    padding-left: 40rpx;
    font-weight: 700;
    line-height: 60rpx;
  }
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: $themeColor;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;
            padding-right: 20rpx;
            display: flex;
            justify-content: flex-end;
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;

    .all-price {
      flex: 1;
      color: #666;
    }
  }
}
</style>